package com.forestar.mapControl.mapview;

import android.graphics.Bitmap;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;

import com.forestar.mapControl.utils.mapConvert.MapViewTransform;
import com.forestar.mapControl.utils.mapConvert.geometry.GeoPoint;


public class CacheCanvas
  {

    private Canvas canvas;
    private Bitmap cacheBitmap;
    public Object lockObject = new Object();
    private MapViewTransform transform;
    private Paint paint;
    private int renderSeialId;
    private boolean isInited;
    //原始宽高 即MapControl的宽高
    private int originalHeight;
    private int originalWidth;
    private int rollingShutterMode;//卷帘模式   0:没开启卷帘  1:右往左滑动   2:左往右滑动
    private double rollingShutterProportion; //卷帘模式隐藏的比例
    private Paint rollingShutterPaint;//卷帘用画笔

    public CacheCanvas()
      {
        paint = new Paint();
        rollingShutterPaint = new Paint();
        rollingShutterPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
      }

    public void reSize(MapViewTransform transform, int width, int height)
      {
        this.isInited = true;
        this.transform = transform;
        if (width <= 0)
          {
            width = 1;
          }
        if (height <= 0)
          {
            height = 1;
          }
        this.cacheBitmap = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
        this.canvas = new Canvas(cacheBitmap);
      }

    public void clear()
      {
        // 清屏
        paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        canvas.drawPaint(paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC));

        //TODO:
        //        cacheBitmap.recycle();
        //        cacheBitmap = null;
        //        System.gc();
      }

    public void clear(Canvas c)
      {
        // 清屏
        paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        c.drawPaint(paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
      }

    public void setTransform(MapViewTransform transform)
      {
        this.transform = transform.clone();
      }

    public Canvas getCanvas()
      {
        return canvas;
      }

    public Bitmap getBitmap()
      {
        return cacheBitmap;
      }

    public void draw(CacheCanvas cacheCanvas)
      {
        this.clear();
        Bitmap bitmap = this.cacheBitmap;
        this.canvas.drawBitmap(bitmap, 0, 0, paint);
        this.setSerialId(cacheCanvas.getSerialId());
        this.setTransform(cacheCanvas.transform);
      }

    public void drawCover(CacheCanvas cacheCanvas)
      {
        Bitmap bitmap =  this.cacheBitmap;
        this.canvas.drawBitmap(bitmap, 0, 0, paint);
        this.setSerialId(cacheCanvas.getSerialId());
        this.setTransform(cacheCanvas.transform);
      }

    Rect dstRect = new Rect();
    Rect srcRect = new Rect();

    public void drawSkew(CacheCanvas oriCacheCanvas)
      {
        this.clear();
//        Bitmap bitmap = oriCacheCanvas.getBitmap();
        //计算、绘制瓦片缓存
        MapViewTransform oriTrans = oriCacheCanvas.getTransform();
        double resolutionScale = 0;
        int width = 0;
        int height = 0;
        PointF oriPointF;
        GeoPoint geoPoint = transform.screenPoint2MapPoint(0, 0);
        oriPointF = oriTrans.mapPoint2ScreenPoint(geoPoint);
        dstRect.left = 0;
        dstRect.top = 0;
        dstRect.right = (int) (transform.getScreenCenterX() * 2);
        dstRect.bottom = (int) (transform.getScreenCenterY() * 2);
        resolutionScale = (transform.getResolutionInMapUnit() / oriTrans.getResolutionInMapUnit());
        width = (int) (transform.getScreenCenterX() * 2 * resolutionScale);
        height = (int) (transform.getScreenCenterY() * 2 * resolutionScale);
        srcRect.left = (int) oriPointF.x;//放大地图view的宽度，解决计算误差导致的边界白边的问题，放大10个像素，10是经验值
        // (之前是3，在平板上测试发现有偏7个像素的情况，现在改为10);
        srcRect.top = (int) oriPointF.y;
        srcRect.right = (int) oriPointF.x + width;// 放大地图view的宽度，解决计算误差导致的边界白边的问题，放大10个像素，10是经验值;
        srcRect.bottom = (int) oriPointF.y + height;
        // this.cacheBitmap;
        canvas.drawBitmap(oriCacheCanvas.getBitmap(), srcRect, dstRect, paint);
        this.setSerialId(oriCacheCanvas.getSerialId());
      }

    public void drawToBitmap(Bitmap bitmap)
      {
        if (null == this.cacheBitmap)
          return;
        if (null == bitmap)
          {
            bitmap = Bitmap.createBitmap(this.cacheBitmap);
          } else
          {
            Canvas c = new Canvas(bitmap);
            clear(c);
            c.drawBitmap(this.cacheBitmap, 0, 0, null);
          }
      }

    public MapViewTransform getTransform()
      {
        return transform;
      }

    public void setSerialId(int renderSerialId)
      {
        this.renderSeialId = renderSerialId;
      }

    public int getSerialId()
      {
        return this.renderSeialId;
      }

    public boolean isInited()
      {
        return isInited;
      }

    public int getOriginalHeight()
      {
        return originalHeight;
      }

    public void setOriginalHeight(int originalHeight)
      {
        this.originalHeight = originalHeight;
      }

    public int getOriginalWidth()
      {
        return originalWidth;
      }

    public void setOriginalWidth(int originalWidth)
      {
        this.originalWidth = originalWidth;
      }

    public Paint getPaint()
      {
        return paint;
      }

    public int getRollingShutterMode()
      {
        return rollingShutterMode;
      }

    public void setRollingShutterMode(int rollingShutterMode)
      {
        this.rollingShutterMode = rollingShutterMode;
      }

    public void setRollingShutterProportion(double rollingShutterProportion)
      {
        this.rollingShutterProportion = rollingShutterProportion;
      }

      //绘制卷帘透明区域
    public void drawRollingShutter()
      {
        if (rollingShutterMode == 0)
          {
            return;
          } else if (rollingShutterMode == 2)
          {
            canvas.drawRect(0, 0, (int) (originalWidth * rollingShutterProportion), originalHeight, rollingShutterPaint);
          } else if (rollingShutterMode == 1)
          {
            canvas.drawRect((int) (originalWidth * rollingShutterProportion), 0, originalWidth, originalHeight, rollingShutterPaint);
          } else
          {
            return;
          }
      }

  }
